package com.shoujifeng.snowmusic.player.http;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

public class PostAccess
{
	/**
	 * SendPostData 	- 发送POST到服务端
	 * @param keys		- Keys
	 * @param values	- 键值ֵ
	 * @param postUrl	- Post地址ַ
	 * @return
	 */
	public String sendPostData(String[] keys, String[] values, String postUrl)
	{
		String reString = null;
		
		HttpPost httpPost = new HttpPost(postUrl);
		List<NameValuePair> parms = new ArrayList<NameValuePair>();
		
		for(int i = 0; i < keys.length; i++)
		{
			parms.add(new BasicNameValuePair(keys[i], values[i]));
		}
		
		try
		{
			httpPost.setEntity(new UrlEncodedFormEntity(parms, HTTP.UTF_8));
			
			HttpClient client = new DefaultHttpClient();

            client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000);

			HttpResponse httpResponse = client.execute(httpPost);
			
			if(httpResponse.getStatusLine().getStatusCode() == 200)
			{
				reString = EntityUtils.toString(httpResponse.getEntity(), HTTP.UTF_8).trim();
			}

		} catch (Exception e)
		{
				System.out.println(e.getMessage());
			
		}
		
		return reString;
	}
}
